<template>
  <div>
    <zh-form  ref="formInfo" :colmun="1" :rules="rules" :model="formInfo"  labelAlign="right" >

      <zh-card style="width:100%">
        <zh-row>
          <zh-input style="float: left"  type="text"  :span="22"  prop="d0702" v-model="formInfo.d0702" label="任务名称"
                    :labelCol="{span:3}" :wrapperCol="{span:21}"/>
        </zh-row>
        <zh-row>
          <a-col :span="22">
            <a-form-model-item label="任务类型" prop="d0706" :labelCol="{span:3}" :wrapperCol="{span:18}">
              <a-radio-group :disabled="editAble" name="d0706Radio" v-model="formInfo.d0706" @change="taskTypeChange">
                <a-radio value="1">常态任务</a-radio>
                <a-radio value="2">专项任务</a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
          <!--<div style="margin-bottom: 20px">-->
          <!--  <label style="color: black;margin-left: 35px">任务类型：</label>-->
          <!--  <a-radio-group :disabled="editAble" name="d0706Radio" v-model="formInfo.d0706" @change="taskTypeChange">-->
          <!--    <a-radio value="1">常态任务</a-radio>-->
          <!--    <a-radio value="2">专项任务</a-radio>-->
          <!--  </a-radio-group>-->
          <!--</div>-->
        </zh-row>
        <zh-row >
          <zh-input :span="11" prop="d0703" :disabled="editAble" :disabledDate="onDisabledDate" @change="d0703Change"  type="date"
                    label="开始时间" v-model="formInfo.d0703" :labelCol="{span:6}" :wrapperCol="{span:18}"></zh-input>
          <zh-input :span="11" prop="d0704" :disabled="false" :disabledDate="onDisabledDate" @change="d0704Change" type="date" label="结束时间" v-model="formInfo.d0704"  :labelCol="{span:6}" :wrapperCol="{span:18}"
                    style="margin-left: 15px"></zh-input>
        </zh-row >
        <zh-row>
          <zh-input style="float: left" prop="c0303" :span="22" type="select" :defaultFields='{label:"dictValue",value:"dictKey",}'
                    :options="dictData.c0303" :disabled="editAble" label="资源表单" v-model="formInfo.c0303"
                    :labelCol="{span:3}" :wrapperCol="{span:9}" @change="c0303Change"></zh-input>
        </zh-row>
        <zh-row>
          <patrol-task ref="patrolTaskSetting" :span="22" label="巡查设置" :form-info="this.formInfo"
                       @patrolDataChange="getPatrolChangeData"  prop="patrolTask" :labelCol="{ span:3 }"  :wrapperCol="{ span:21 }"></patrol-task>
        </zh-row>
<!--        <label style="color: black;font-size: 16px;margin-left: 35px">巡查设置：</label>
        <div v-for="(item, index) in patrolSetData" :key="index" style="margin-top: 15px">
          <div style="float: right">
            <ep-button @click="editPatrolSet(index,item)">编辑</ep-button>
            <ep-button style="margin-left: 5px" type="danger" icon="del.svg" @click="delPatrolSet(index,item)">删除</ep-button>
            <ep-button style="margin-left: 5px" v-if="item.pointCount>0"
                       @click="showTasksList(index,item)">{{item.pointCount}}处</ep-button>
          </div>
          <a-descriptions  bordered  :column="2">
            <a-descriptions-item label="区域">{{ item.d0206 }}</a-descriptions-item>
            <a-descriptions-item label="文物等级">{{ item.d0215Name }}</a-descriptions-item>
            <a-descriptions-item label="巡查级别">{{ item.d0216Name }}</a-descriptions-item>
            <a-descriptions-item label="类别">{{ item.d0217Name }}</a-descriptions-item>
            <a-descriptions-item label="巡查频率">{{ item.patrolFrequency }}</a-descriptions-item>
          </a-descriptions>
          <div style="margin-top: 10px"></div>
        </div>
        <br/>
        <a-button @click="addPatrolSetData" style="width: 75%;margin-left: 30px" icon="add" type="primary" block>{{this.patrolSetTitle}}</a-button>-->
      </zh-card>

    </zh-form>


<!--    <a-modal
        title="巡查设置"
        centered
        style="top: 20px"
        width="55%"
        height="100%"
        destroyOnClose
        v-model="visible"
        cancelText="关闭"
        @ok="handleOk"
        @cancel="handleCancel"
    >
      <patrol-set-details-form
          ref="patrolSetDetailsForm"
          @handleCancel="handleCancel"
          @showOrHide="showOrHide"
          :set-patrol-data="patrolDataChange">
      </patrol-set-details-form>
      <template slot="footer">
        <ep-button @click="handleOk" >确认</ep-button>
        <ep-button @click="handleCancel">取消</ep-button>
      </template>
    </a-modal>

    <a-modal
        title="任务清单"
        centered
        style="top: 20px"
        width="75%"
        height="95%"
        destroyOnClose
        v-model="visible2"
        cancelText="关闭"
        @ok="handleOk"
        @cancel="handleCancel"
    >
      <patrol-point-list-form
          ref="patrolPointListForm"
          @handleCancel="handleCancel2"
          @showOrHide="showOrHide2">
      </patrol-point-list-form>

      <template slot="footer">
        <a></a>
      </template>
    </a-modal>-->
  </div>
</template>

<script>
import modal from 'ant-design-vue/es/modal'
import { getBatchDict} from "@/services/system/api"
import {patrolTaskAdd,patrolTasksAddAndOpen,getPatrolTaskDel,getAllPointCount
} from '@/services/system/whdngxlApi'
import moment from "moment";
import PatrolTask from "@/pages/system/form/PatrolTask";
export default {
  components:{
    PatrolTask
  },
  props:{
    listQuery:{
      type:Function,
      default:null
    }
  },
  data() {
    return {
      formInfo:{
        d0701:'',
        d0702:'',
        d0703:'',
        d0704:null,
        c0303:'',
        resourcesData:[],//子页面勾选的巡查点数据
        d0706:'',
      },
      patrolSetData: [],
      patrolSetDataDel:[],
      rules:{},
      dictData: {
        c0303:[],
      },
      visible: false,
      visible2: false,
      patrolSetTitle:'添加',
      editIndex: "",
      settingType:"",
      allPointCount:null, //总点位数量
      choosePointCount:null, //已选点位数量
      surplusPointCount:null, //剩余点位数量
      editAble:false,

    }
  },

  created() {
    const param = {}
    param.dictFields = 'c0303'
    this.$http.post(getBatchDict, param).then(res => {
      this.dictData = res.result
    })
  },
  methods: {
    initData(row){
      if(row){
        this.editAble = true
        const param = {}
        param.d0701 = row.d0701
        this.$http.post(getPatrolTaskDel, param).then(res => {
          if(res.result){
            this.formInfo.d0701 = res.result.d0701
            this.formInfo.d0702 = res.result.d0702
            this.formInfo.d0703 = res.result.d0703
            this.formInfo.d0704 = res.result.d0704
            this.formInfo.c0303 = res.result.c0303
            this.formInfo.d0706 = res.result.d0706
            this.patrolSetData = res.result.patrolSetData
            this.allPointCount = res.result.allPointCount
            this.choosePointCount = res.result.choosePointCount
            this.surplusPointCount = res.result.allPointCount - res.result.choosePointCount
            if(this.choosePointCount>0){
              this.patrolSetTitle = '还有'+this.surplusPointCount+'处文保单位未分配任务，继续分配'
            }
            const data = {}
            data.patrolSetData = this.patrolSetData
            data.patrolSetDataDel = this.patrolSetDataDel
            data.allPointCount = this.allPointCount
            data.choosePointCount = this.choosePointCount
            data.surplusPointCount = this.surplusPointCount
            data.patrolSetTitle = this.patrolSetTitle
            this.$refs.patrolTaskSetting.initData(data);
          }
        })
      }
    },
    /*addPatrolSetData(){
      if(!this.formInfo.d0706){
        modal.warn({title: '提示', content: '请先选择任务类型'})
        return
      }
      if(!this.formInfo.c0303){
        modal.warn({title: '提示', content: '请先选择资源表单'})
        return
      }
      if(!this.formInfo.d0703){
        modal.warn({title: '提示', content: '请先选择开始时间'})
        return
      }
      this.settingType="新增"
      this.showOrHide();
      this.$nextTick(() => {
        const data = {}
        data.d0706 = this.formInfo.d0706
        data.c0303 = this.formInfo.c0303
        data.d0703 = this.formInfo.d0703
        data.d0704 = this.formInfo.d0704
        this.$refs.patrolSetDetailsForm.initData(data);
      });
    },*/
    //日期选择器禁用的日期
    onDisabledDate(current){
      const time = moment().endOf('day').subtract(1, 'days')
      return current < time
    },
    c0303Change(){
      this.patrolSetData = []
      this.patrolSetDataDel = []
      if(this.formInfo.c0303){
        const params = Object.assign(this.formInfo)

        this.$refs.patrolTaskSetting.c0303Change(params);
      }
    },
    //任务类型事件
    taskTypeChange(e){
      this.patrolSetData = []
      this.patrolSetDataDel = []
      if(this.formInfo.d0706){
        const params = Object.assign(this.formInfo)

        this.$refs.patrolTaskSetting.taskTypeChange(params);

      }
      // console.log('radio1 checked', e.target.value);
    },
    getPatrolChangeData(patrolDataChange){
      this.patrolSetData = patrolDataChange.patrolSetDataChild
      this.choosePointCount = patrolDataChange.choosePointCountChild
      this.surplusPointCount = patrolDataChange.surplusPointCountChild
      this.patrolSetTitle = patrolDataChange.patrolSetTitleChild
    },
    /*patrolDataChange(data){
      if(data.flag === "edit"){
        this.settingType = "编辑"
      }
      if(this.settingType === "编辑"){
        this.patrolSetData.splice(this.editIndex, 1, data);
      }else{
        this.patrolSetData.push(data)
      }
      this.choosePointCount += data.pointCount
      this.surplusPointCount = this.allPointCount-this.choosePointCount
      this.patrolSetTitle = '还有'+this.surplusPointCount+'处文保单位未分配任务，继续分配'
    },
    showTasksList(index,item){
      this.visible2 = true;
      this.settingType = "编辑"
      this.editIndex = index;
      item.d0706 = this.formInfo.d0706
      item.c0303 = this.formInfo.c0303
      item.c0703 = this.formInfo.d0703
      item.c0704 = this.formInfo.d0704
      this.$nextTick(() => {
        this.$refs.patrolPointListForm.initData(item);
      })
    },
    editPatrolSet(index,item){
      if(!this.formInfo.d0706){
        modal.warn({title: '提示', content: '请先选择任务类型'})
        return
      }
      if(!this.formInfo.c0303){
        modal.warn({title: '提示', content: '请先选择资源表单'})
        return
      }
      if(!this.formInfo.d0703){
        modal.warn({title: '提示', content: '请先选择开始时间'})
        return
      }
      this.settingType="编辑"
      this.visible = true;
      this.editIndex = index;
      this.$nextTick(() => {
        if (item.d0201) {
          item.flag = "edit";
        }
        item.d0706 = this.formInfo.d0706
        item.c0303 = this.formInfo.c0303
        item.c0703 = this.formInfo.d0703
        item.c0704 = this.formInfo.d0704
        this.$refs.patrolSetDetailsForm.initData(item);
      });
    },
    delPatrolSet(index,item){
      modal.confirm({
        title: "确认删除吗？",
        onOk: () => {
          if (this.patrolSetData[index].d0201) {
            const item = JSON.parse(JSON.stringify(this.patrolSetData[index]));
            item.flag = "delete";
            this.patrolSetDataDel.push(item);
          }
          this.patrolSetData.splice(index, 1);
          if(item.pointCount){
            this.surplusPointCount = this.surplusPointCount+item.pointCount
            this.patrolSetTitle = '还有'+this.surplusPointCount+'处文保单位未分配任务，继续分配'
          }
          if(this.patrolSetData.length === 0){
            this.patrolSetTitle = '添加'
          }
        },
        onCancel: () => {},
      })
    },*/
    taskSave(type){
      if(!this.formInfo.d0702){
        modal.warn({title: '提示', content: '任务名称不能为空'})
        return
      }
      if(!this.formInfo.d0706){
        modal.warn({title: '提示', content: '任务类型不能为空'})
        return
      }
      if(!this.formInfo.c0303){
        modal.warn({title: '提示', content: '资源表单不能为空'})
        return
      }
      if(!this.formInfo.d0703){
        modal.warn({title: '提示', content: '开始时间不能为空'})
        return
      }
      if(this.patrolSetData.length === 0){
        modal.warn({title: '提示', content: '巡查设置数据不能为空'})
        return
      }
      if(this.formInfo.d0703 && this.formInfo.d0704){
        let d0703 = this.formInfo.d0703.replaceAll('-','');
        let d0704 = this.formInfo.d0704.replaceAll('-','');
        if(d0703>d0704){
          this.formInfo.d0703 = ''
          this.formInfo.d0704 = ''
          modal.warn({title: '提示', content: '结束时间不能小于开始时间'})
        }
      }
      debugger
      let postUrl = patrolTaskAdd
      if(type===2){ //保存并开启
        postUrl = patrolTasksAddAndOpen
      }
      this.$loading.show('正在处理数据,请等待...')
      const params = Object.assign(this.formInfo)
      params.patrolSetData = this.patrolSetData
      params.patrolSetDataDel = this.patrolSetDataDel
      setTimeout(() => {
        this.$http.post(postUrl, params).then((res) => {
          this.$message.info('保存成功')
          this.$emit("handleCancel")
          if(this.listQuery){
            this.listQuery()
          }
          this.$loading.hide()
        }).catch((error) => {
          console.log(JSON.stringify(error))
          this.$loading.hide()
        }).finally(() =>{
        })
      }, 50)
    },
    handleCancel() {
      this.visible = false;
    },
    showOrHide() {
      this.visible = !this.visible;
    },
    handleCancel2() {
      this.visible2 = false;
    },
    showOrHide2() {
      this.visible2 = !this.visible2;
    },
    handleOk() {
      this.$nextTick(() => {
        this.$refs.patrolSetDetailsForm.confirm();
      });
    },
    d0703Change(){
      if(this.formInfo.d0703 && this.formInfo.d0704){
          let d0703 = this.formInfo.d0703.replaceAll('-','');
          let d0704 = this.formInfo.d0704.replaceAll('-','');
          if(d0703>d0704){
            this.formInfo.d0703 = ''
            this.formInfo.d0704 = ''
            modal.warn({title: '提示', content: '结束时间不能小于开始时间'})
          }
      }
    },
    d0704Change(){
      if(this.formInfo.d0703 && this.formInfo.d0704){
        let d0703 = this.formInfo.d0703.replaceAll('-','');
        let d0704 = this.formInfo.d0704.replaceAll('-','');
        if(d0703>d0704){
          this.formInfo.d0703 = ''
          this.formInfo.d0704 = ''
          modal.warn({title: '提示', content: '结束时间不能小于开始时间'})
        }
      }
    },
  }
}
</script>

<style scoped>

</style>
